﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Functions;
using SchoolProject2012.Areas.Eleve.Models;


namespace SchoolProject2012.Areas.Eleve.Controllers
{
    public class ContactController : Controller
    {
        private EleveSchoolProjectBddEntities db = new EleveSchoolProjectBddEntities();
        //
        // GET: /Eleve/Contact/
        // Affiche les actions possibles
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                return View();
            }
            catch
            {
                
                return View("error");
            }
        }

        // GET: /Eleve/contact/ContactCDP
        // Formulaire de contact à un chargé de placement
        [Authorize(Roles="Admin, Eleve, Delegue, BDE")]
        public ViewResult ContactCDP(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                string to = db.ELEVE.Single(x => x.LOGINPERS == login).CHARGEDEPLACEMENT.MAILPERS;
                ViewData["adresse"] = to;
                ViewBag.langue = langue;
                return View();
            }
            catch
            {
                
                return View("error");
            }
        }

        // POST: /Eleve/contact/ContactCDP
        // Validation du formulaire de contact
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        [HttpPost]
        public ActionResult ContactCDP(string langue, string subject, string message)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                ELEVE e = db.ELEVE.Single(x => x.LOGINPERS == login);
                string email = db.ELEVE.Single(x => x.LOGINPERS == login).CHARGEDEPLACEMENT.MAILPERS;
                List<string> to = new List<string>();
                to.Add("s.valentin77@gmail.com");
                Mail.sendMail(subject, e.MAILPERS, to, message);
                return RedirectToAction("index");
            }
            catch
            {
                return View("error");
            }
        }

        // GET: /Eleve/contact/Contactclasse
        // Formulaire de contact à la classe
        [Authorize(Roles = "Admin, Delegue, BDE")]
        public ViewResult ContactClasse(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                return View();
            }
            catch
            {
                
                return View("error");
            }
        }

        // POST: /Eleve/contact/Contactclasse
        // Validation du formulaire
        [Authorize(Roles = "Admin, Delegue, BDE")]
        [HttpPost]
        public ActionResult ContactClasse(string langue, string subject, string message)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                ELEVE e = db.ELEVE.Single(x => x.LOGINPERS == login);
                var eleveclass = e.ELEVECLASS.Where(c => c.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).Select(x => x.ELEVE.MAILPERS).ToList();
                List<string> to = null;
                foreach (string adr in eleveclass)
                {
                    to.Add(adr);
                }
                Mail.sendMail(subject, e.MAILPERS, to, message);
                return RedirectToAction("Index");
            }
            catch
            {
                
                return View("error");
            }
        }
    }
}
